package in.muton.edushare.pages.batch;

import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.common.UserFilter;
import in.muton.edushare.pages.curricula.CurriculaSummary;
import in.muton.edushare.pages.deptpages.DeptBatches;
import in.muton.edushare.pages.univ.UnivBatches;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;
import in.muton.panels.paginator.badgehelpers.user.BatchBadgeRoles;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Users;

public class BatchSummary extends BatchBase {

	public BatchSummary(final PageParameters params) {
		super(params);
		Link l;
		PageParameters pageParams = new PageParameters();
		pageParams.put("0", Integer.toString(batchModel.getObject().getCurricula().getCurriculaId()));
		add(l = new BookmarkablePageLink<Void>("currUrl",CurriculaSummary.class, pageParams));
		l.add(new Label("currName", batchModel.getObject().getCurricula()
				.getCurriculaName()));
		add(new EduShareURLPanel("batchDept", new CayenneModel<Departments>(
				batchModel.getObject().getCurricula().getDepartment())));
		add(new Label("start", batchModel.getObject().getStartingSemester()
				.getSemesterName()+", " + batchModel.getObject().getBatchYear()));
		add(new Label("studentNum", Integer.toString(new BatchDAO(
				getSessionORMInterface()).getUsers(batchModel.getObject())
				.size())));

		add(new Link("invite") {
			@Override
			public void onClick() {
				setResponsePage(BatchPeople.class, params);
			}

			@Override
			public boolean isVisible() {
				DeptRoleCheck drc = new DeptRoleCheck(batchModel.getObject()
						.getCurricula().getDepartment(), getLoggedInUser());
				if (!(drc.isPrimaryAdmin() || drc.isUnivAdmin()))
					return false;
				return !(batchModel.getObject().getBatchStatus() == Batches.BATCH_GRADUATED);
			}

			private static final long serialVersionUID = 5364190865412993242L;
		});
		
		BatchBadgeRoles br = new BatchBadgeRoles(batchModel);
		add(new EntityListing<Users>("thumbList", new InnerList<Users>() {
			private static final long serialVersionUID = -56856143743214954L;

			@Override
			public List<IModel<Users>> getObject() {
				return getUsers().getObject();
			}
		}, br, this));

		Form<Void> f = new Form<Void>("searchForm");
		f.add(new TextField<String>("searchString", new PropertyModel<String>(
				this, "searchString")));
		add(f);

		/*Form<Void> f2 = new Form<Void>("dummyForm") {
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				params.put("0", batchModel.getObject().getCurricula()
						.getDepartment().getDeptName());
				params.put("1", Integer.toString(batchModel.getObject()
						.getCurricula().getDepartment().getDepartmentId()));
				setResponsePage(DeptBatches.class, params);
			}

			private static final long serialVersionUID = 5364190865412993242L;
		};
		add(f2);*/
		if(fromPage==fromUnivPage){
			add(new BookmarkablePageLink<Void>("finishedReview", UnivBatches.class, getUnivPagesParamaters()));
		}else{
			add(new BookmarkablePageLink<Void>("finishedReview", DeptBatches.class, getDeptPagesParamaters()));
		}
	}

	private CayenneList<Users> getUsers() {
		CayenneList<Users> cL = new CayenneList<Users>(null);
		cL.add(new BatchDAO(getSessionORMInterface()).getUsers(batchModel
				.getObject()));
		cL = UserFilter.filterByString(cL, searchString);
		return cL;
	}

	@Override
	public String getPageTitle() {
		return batchModel.getObject().getBatchName()
				+ " | Details | " + ApplicationConstants.APPLICATION_NAME;
	}


	private String searchString = "";

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	@Override
	public String getActivePage() {
		return "batchDetails";
	}

}
